package com.itfreer.gis.arcgis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itfreer.gis.graphic.GisGraphic;
import com.itfreer.gis.graphic.GisLine;
import com.itfreer.gis.graphic.GisPoint;
import com.itfreer.gis.graphic.GisPolyg;

public class ArcGisUtils {
	 /**
	   * 处理照片路径
	   *
	   * @param attachments
	   * @return
	   */
	 /* public static StringBuffer processPhotosAddStr(List<Attachment> attachments, String type) {
	    StringBuffer photoStr = new StringBuffer();
	    String devType = "";
	    if (type.equals(ArcGisConstants.BOX)) {
	      devType = ArcGisConstants.BOX_PHOTOS;
	    }
	    if (attachments != null && attachments.size() > 0) {
	      for (int i = 0; i <= attachments.size() - 1; i++) {
	        photoStr.append(attachments.get(i).getAddress().split(devType)[1]);
	        if (i < attachments.size() - 1) {
	          // 不是最后一张
	          photoStr.append(",");
	        }
	      }
	    }
	    return photoStr;
	  }*/

	  /**
	   * 创建新的容器模型
	   *
	   * @param geometry
	   * @param attributes
	   * @return
	   */
	  public static List<Map<String, Object>> createNewFeatures(Map<String, Object> geometry, Map<String, Object> attributes) {
	    List<Map<String, Object>> features = new ArrayList<>();
	    HashMap<String, Object> feature = new HashMap<>();
	    feature.put(ArcGisConstants.GEOMETRY, geometry);
	    feature.put(ArcGisConstants.ATTRIBUTES, attributes);
	    features.add(feature);
	    return features;
	  }
	  
	  /**
	   * 构建arcgis图形对象
	   * @param graphic
	   * @return
	   */
	  public static Map<String, Object> createNewGeometry(GisGraphic graphic){
		  if(graphic instanceof GisPoint) {
			  return createNewGeometry((GisPoint)graphic);
		  }else if(graphic instanceof GisPolyg) {
			  return createNewGeometry((GisPolyg)graphic);
		  }else if(graphic instanceof GisLine) {
			  return createNewGeometry((GisLine)graphic);
		  }else {
			  return null;
		  }
	  }
	  
	  /**
	   * 构建点图形
	   * @param point
	   * @return
	   */
	  public static Map<String, Object> createNewGeometry(GisPoint point) {
		  Map<String, Object> newGeometry = new HashMap<String, Object>();
		  newGeometry.put(ArcGisConstants.LON, point.getX());
		  newGeometry.put(ArcGisConstants.LAT, point.getY());
		  
		  Map<String, Object> spatialReference = new HashMap<String, Object>();
		  spatialReference.put("wkid", point.getCoordinate());
		  
		  newGeometry.put("spatialReference",spatialReference );
		  return newGeometry;
	  }
	  
	  /**
	   * 构建面对象
	   * @param polyg
	   * @return
	   */
	  public static Map<String, Object> createNewGeometry(GisPolyg polyg) {
		  Map<String, Object> newGeometry = new HashMap<String, Object>();
		  List<Object> points=new ArrayList<>();
		  for(GisPoint ipoint:polyg.getPoints()) {

			  List<Double> zb=new ArrayList<>();
			  zb.add(ipoint.getX());
			  zb.add(ipoint.getY());
			  points.add(zb);
		  }
		  List<Object> geometrys=new ArrayList<>();
		  geometrys.add(points);
		  newGeometry.put("rings", geometrys);

		  Map<String, Object> spatialReference = new HashMap<String, Object>();
		  spatialReference.put("wkid", polyg.getCoordinate());
		  
		  newGeometry.put("spatialReference",spatialReference );
		  return newGeometry;
	  }
	  
	  /**
	   * 构建线对象
	   * @param polyg
	   * @return
	   */
	  public static Map<String, Object> createNewGeometry(GisLine line) {
		  Map<String, Object> newGeometry = new HashMap<String, Object>();
		  String str="";
		  for(GisPoint ipoint:line.getPoints()) {
			  if("".equals(str)) {
				  str=str+"["+ipoint.getX()+","+ipoint.getY()+']';
			  }else {
				  str=str+",["+ipoint.getX()+","+ipoint.getY()+']';
			  }
		  }
		  if(!"".equals(str)) {
			  str="[["+str+"]]";
		  }
		  newGeometry.put("paths", str);
		  Map<String, Object> spatialReference = new HashMap<String, Object>();
		  spatialReference.put("wkid", line.getCoordinate());
		  
		  newGeometry.put("spatialReference",spatialReference );
		  return newGeometry;
	  }
}
